package com.yaozheng.vocationaltraining.utils;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
 * 题目帮助类
 *
 * @author Administrator
 */
public class QuestionUtils {
    /**
     * 得到答案
     *
     * @param questionArray
     * @return
     */
    public static String getAnswers2222(JSONArray questionArray) {
        boolean isSubmitAnswer = false;
        StringBuffer answers = new StringBuffer("{");

        for (int i = 0; i < TypeUtils.getJsonArraySize(questionArray); i++) {
            JSONObject questionJsonObject = TypeUtils.getJsonObject(
                    questionArray, i);
            if (i > 0) {
                answers.append(";");
            }
            String answer = TypeUtils.getJsonString(questionJsonObject,
                    "answer");
            if (answer != null && !"".equals(answer)) {
                isSubmitAnswer = true;
            } else {
                break;
            }
            if (answer.indexOf(",") == -1) {

                answers.append(TypeUtils.getJsonString(questionJsonObject, "id"));
                answers.append("=");
                answers.append("[" + answer + "]");
//				answers.put(TypeUtils.getJsonString(questionJsonObject, "id"),
//						"[\"" + answer + "\"]");
            } else {
                StringBuffer multipleAnswerBuffer = null;
                String[] multipleAnswers = answer.split(",");
                for (int j = 0; j < multipleAnswers.length; j++) {

                    String multipleAnswer = multipleAnswers[j];
                    if (multipleAnswerBuffer == null) {
                        multipleAnswerBuffer = new StringBuffer("(");
                    } else {
                        multipleAnswerBuffer.append(",");
                    }
                    multipleAnswerBuffer.append(multipleAnswer);

                }
                if (multipleAnswerBuffer != null) {
                    multipleAnswerBuffer.append(")");

                    answers.append(TypeUtils.getJsonString(questionJsonObject, "id"));
                    answers.append("=");
                    answers.append(multipleAnswerBuffer.toString());
                }
            }
        }
        if (isSubmitAnswer) {
            answers.append("}");
            return answers.toString();
        }
        return null;
    }

    public static String getAnswers(JSONArray questionArray) {
        boolean isSubmitAnswer = false;
        JSONObject answers = new JSONObject();
        for (int i = 0; i < TypeUtils.getJsonArraySize(questionArray); i++) {
            JSONObject questionJsonObject = TypeUtils.getJsonObject(
                    questionArray, i);
            String answer = TypeUtils.getJsonString(questionJsonObject,
                    "answer");
            if (answer != null && !"".equals(answer)) {
                isSubmitAnswer = true;
            } else {
                break;
            }
            if (answer.indexOf(",") == -1) {
                answers.put(TypeUtils.getJsonString(questionJsonObject, "id"),
                        "[\"" + answer + "\"]");
            } else {
                StringBuffer multipleAnswerBuffer = null;
                String[] multipleAnswers = answer.split(",");
                for (int j = 0; j < multipleAnswers.length; j++) {
                    String multipleAnswer = multipleAnswers[j];
                    if (multipleAnswerBuffer == null) {
                        multipleAnswerBuffer = new StringBuffer("[");
                    } else {
                        multipleAnswerBuffer.append(",");
                    }
                    multipleAnswerBuffer.append("\"");
                    multipleAnswerBuffer.append(multipleAnswer);
                    multipleAnswerBuffer.append("\"");

                }
                if (multipleAnswerBuffer != null) {
                    multipleAnswerBuffer.append("]");
                    answers.put(
                            TypeUtils.getJsonString(questionJsonObject, "id"),
                            multipleAnswerBuffer.toString());
                }
            }
        }
        if (isSubmitAnswer) {
            return answers.toString();
        }
        return null;
    }

}
